<?php
class api extends base{

	public function init(){ // 公用
		parent::init(); // 这是必须的

		$this->upload_groupObj = D('upload_group');
		$this->upload_fileObj = D('upload_file');

         // 存储引擎
		$this->config = $this->setting['storage'];
		$this->storage = $this->config['default'];
		$this->storageObj = $this->upload_fileObj->checkStorage($this->config);
	}


	public function fileList()
    {
		$type = arg('type') ?? 'image';
		$group_id = arg('group_id') ?? 0;
		// 分组列表
		$cond = "group_type='{$type}' and status=1 ".$this->cond_base;
		$group_list = $this->upload_groupObj->getList($cond);
		// 文件列表
		$cond = "group_id='{$group_id}' and file_type='{$type}' and status=1 ".$this->cond_base;
		$file_list = $this->upload_fileObj->getlist($cond);
		
		$this->renderSuccess('success', '', compact('group_list', 'file_list'));
		
	}
	
    public function addGroup()
    {
		$group_name = arg('group_name');
		$group_type = arg('group_type') ?? 'image';

		$arrBase = array(
			'site_id'=>$this->site['id'],
			'group_name'=>arg('group_name'),
			'group_type'=>arg('group_type') ?? 'image',
		);
		// $data['site_id'] = $this->site['id'];
		$group_id = $this->upload_groupObj->add($arrBase);
        if ($group_id) {
            $this->renderSuccess('添加成功', '', compact('group_id', 'group_name'));
        }
        $this->renderError('添加失败');
    }

    public function editGroup()
    {
		$group_id = arg('group_id');
		$data['group_name'] = arg('group_name');

        $res = $this->upload_groupObj->edit($group_id,$data);
        if ($res) {
            $this->renderSuccess('修改成功');
        }
        $this->renderError('修改失败');
    }

    public function deleteGroup()
    {
		$group_id = arg('group_id');
        $res = $this->upload_groupObj->del($group_id);
        if ($res) {
            $this->renderSuccess('删除成功');
        }
        $this->renderError('删除失败');
    }

    public function deleteFiles()
    {
		$fileIds = arg('fileIds');
        $res = $this->upload_fileObj->softDelete($fileIds,0);
        if ($res) {
            $this->renderSuccess('删除成功');
        }
        $this->renderError('删除失败');
    }

    public function moveFiles()
    {
		$group_id = arg('group_id');
		$fileIds = arg('fileIds');
        $res = $this->upload_fileObj->moveGroup($group_id, $fileIds);
        if ($res) {
            $this->renderSuccess('移动成功');
        }
        $this->renderError('移动失败');
	}
	
	public function upload_image()
    {	
		$group_id = arg('group_id');
		$fileInfo = $_FILES['iFile'];

		$result = $this->upload_fileObj->upload_image($this->config,$fileInfo);
		if($result){
			$fileDir = $result['fileDir'];
			$fileName = $result['fileName'];
			// 添加文件库记录
			$uploadFile = $this->addUploadFile($group_id, $fileDir, $fileName, $fileInfo, 'image');
			$this->renderSuccess('图片上传成功', '', $uploadFile);
			exit;
		}else{
			$this->renderError('图片上传失败');
		}

    }

    /**
     * 添加文件库上传记录
     * @param $group_id
     * @param $fileName
     * @param $fileInfo
     * @param $fileType
     * @return UploadFile
     */
	private function addUploadFile($group_id, $fileDir, $fileName, $fileInfo, $fileType)
    {
		// $this->config = $this->setting['storage'];
        // 存储引擎
        $storage = $this->config['default'];
        // 存储域名
        $fileUrl = isset($this->config['engine'][$storage]['domain'])
            ? $this->config['engine'][$storage]['domain'].$fileDir.'/' : $fileDir.'/';
		// 添加文件库记录
		$data = [
			'site_id' => $this->site['id'],
            'group_id' => $group_id > 0 ? (int)$group_id : 0,
            'storage' => $storage,
            'file_url' => $fileUrl,
            'file_name' => $fileName,
            'file_size' => $fileInfo['size'],
            'file_type' => $fileType,
			'extension' => pathinfo($fileInfo['name'], PATHINFO_EXTENSION),
        ];
        $data['file_id'] = $this->upload_fileObj->add($data);
        return $data;
	}
	















    
    public function map_point(){
		$this->mapX = arg('x');
		$this->mapY = arg('y');
		$this->mapW = arg('w');
		$this->mapH = arg('h');
	}

	public function upload() {

		$this->SWFid = session_id();
		$fsinpt = arg('fsinpt');
		$fsdir = arg('fsdir');

		if($this->fmdo=='upload'){
			if($this->setting['oss']['oss_status']){
				$this->uploadOSS();
			}
			
			if(arg('type') == 'type'){
				$_SESSION['uploadType']=arg('utype');
    			exit;
			}

			$upFlie = new UploadFile();
			$upFlie->allow_types = "jpg|gif|png|JPG|GIF|PNG|zip|rar|xls|xlsx";

			if($_SESSION['uploadType']){
				$upFlie->save_path = './upload/'.$_SESSION['uploadType'].'/'.date('Y/md', time());
			}
		    $result = $upFlie->upload_file($_FILES['Filedata']);
		    $msg = $upFlie->errmsg;
		    
		    if($result){
		    	// $this->success=$msg;
		    	// $uploadImg = new uploadImg();
		    	// $ori_img = $uploadImg->img_resized(100,100,$upFlie->uploaded);

				$thumImg = $upFlie->uploaded;
				$thumImg = str_replace('./upload/', '', $thumImg);
				echo $fsinpt.':'.$thumImg;
		    	exit;
		    }else {
		    	$this->errmsg = $msg;
		    }
			
		}
		$fsnum = 5;
		if($fsdir){
			switch($fsdir){
				case "excel":
					$fstype="Excel文件";
					break;
				case "ads":
					$fstype="广告文件";
					break;
				case "news":
					$fstype="资讯图片";
					break;
				case "video":
					$fstype="视频文件";
					break;
				case "cases":
					$fstype="案例图片";
					break;
				default:
					$fstype="默认位置";
					break;
			}
		}else {
			$fsdir="anyone";
		}

		if(arg('fsnum')){
			$fsnum = arg('fsnum');
		}

		$this->fsinpt = $fsinpt;
		$this->fsdir = $fsdir;
		$this->fsnum = $fsnum;
		$this->fstype = $fstype;
	}

	public function uploadOSS() {
		$fsinpt = arg('fsinpt');
		// $file = $_FILES['Filedata'];
		// $result = explode('/',$file['type']);
		// $info = pathinfo($file['name']);
		
		// $file_name = uniqid().'.'.$info['extension'];

		// $upload_dir = "upload/".$this->setting['oss']['oss_dir'];
  //   	$file_dir = $upload_dir.'/'.date('Y/md', time());

  //   	$old_file = $file['tmp_name'];
  //  		$new_file = $file_dir .$file_name;
		// require(APP_DIR.'/protected/include/alioos.php');
  //   	$oos = new alioos();
		// $res = $oos->toOos($old_file, $new_file);
		
		// $oss_url = 'https://quanquanfang.oss-cn-hangzhou.aliyuncs.com/'.$new_file;
		// echo $fsinpt.':'.$new_file;
  //   	exit;
		echo $fsinpt.':1234';
		exit;	
	}

}